<template>
<div>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>请假管理</el-breadcrumb-item>
    <el-breadcrumb-item>请假表</el-breadcrumb-item>
  </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input placeholder="申请人" clearable v-model="eName" @clear="searchLeave">
            <el-button slot="append" icon="el-icon-search" @click="searchLeave"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-input placeholder="请假类型" clearable v-model="type" @clear="searchLeave">
            <el-button slot="append" icon="el-icon-search" @click="searchLeave"></el-button>
          </el-input>
        </el-col>
        <el-col :span="1">
          <el-button type="primary" @click="addLeaveFormVisible=true">添加请假记录</el-button>
        </el-col>
      </el-row>

      <el-table :data="leaveList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="员工编号" prop="eId" ></el-table-column>
        <el-table-column label="员工姓名" prop="eName" ></el-table-column>
        <el-table-column label="请假类型" prop="type" ></el-table-column>
        <el-table-column label="请假时间" prop="startTime" ></el-table-column>
        <el-table-column label="结束时间" prop="endTime" ></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope" >
            <el-button type="warning" size="mini" @click="showEditDialog(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="removeById(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[2, 4 , 8, 16]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
      ></el-pagination>
    </el-card>
  <div>

<!--    添加表单-->
    <el-dialog title="添加请假记录" :visible.sync="addLeaveFormVisible" :close-on-click-modal="false">
      <el-form ref="addChannelFormRef" :rules="addLeaveFormRules" :model="addLeaveForm" label-width="80px" size="mini">

        <el-form-item label="员工工号" prop="eId">
          <el-input v-model="addLeaveForm.eId"></el-input>
        </el-form-item>
        <el-form-item label="员工姓名" prop="eName">
          <el-input v-model="addLeaveForm.eName"></el-input>
        </el-form-item>

        <el-form-item label="请假类型" prop="type">
          <el-input v-model="addLeaveForm.type"></el-input>
        </el-form-item>

        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
              v-model="addLeaveForm.startTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
              v-model="addLeaveForm.endTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAddLeaveDialog">取 消</el-button>
        <el-button type="primary" @click="addLeave">确 定</el-button>
      </div>
    </el-dialog>

<!--    修改表单-->
    <el-dialog title="修改出差记录" :visible.sync="modifyLeaveFormVisible" :close-on-click-modal="false">
      <el-form ref="addChannelFormRef" :rules="modifyLeaveFormRules" :model="modifyLeaveForm" label-width="80px" size="mini">

        <el-form-item label="员工工号" prop="eId">
          <el-input v-model="modifyLeaveForm.eId" disabled></el-input>
        </el-form-item>
        <el-form-item label="员工姓名" prop="eName">
          <el-input v-model="modifyLeaveForm.eName" disabled></el-input>
        </el-form-item>

        <el-form-item label="出差类型" prop="type">
          <el-input v-model="modifyLeaveForm.type"></el-input>
        </el-form-item>

        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
              v-model="modifyLeaveForm.startTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
              v-model="modifyLeaveForm.endTime"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelModifyLeaveDialog">取 消</el-button>
        <el-button type="primary" @click="modifyLeave">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</div>
</template>

<script>
export default {
  name: "Leave",
  data(){
    return{
      eId: '',
      eName: '',
      type: '',
      leaveList: [],
      addLeaveFormVisible: false,
      pageNum: 1,
      pageSize: 2,
      total: 0,
      //添加表单
      addLeaveForm:{
        eId:'',
        eName:'',
        type:'',
        startTime:'',
        endTime:'',
      },
      addLeaveFormRules:{

      },

      //修改表单
      modifyLeaveForm:{
        eId:'',
        eName:'',
        type:'',
        startTime:'',
        endTime:'',
      },
      modifyLeaveFormVisible:false,
      modifyLeaveFormRules:{

      },
    }
  },
  methods:{
    handleSizeChange(size){
      this.pageSize = size;
      this.initLeave();
    },
    handleCurrentChange(now){
      this.pageNum = now;
      this.initLeave();
    },
    searchLeave(){
      this.initLeave();
    },
    initLeave(){
      let url = this.$HRMSUrl + "/leave/leaveList";
      console.log(this.eName)
      this.$axios.get(url, {
        params: {
          eName: this.eName,
          type: this.type,
          now: this.pageNum,
          size: this.pageSize
        }
      }).then(res => {
        if (res.data.code == 200) {
          this.leaveList = res.data.data.list;
          this.pageNum = res.data.data.pageNum;
          this.pageSize = res.data.data.pageSize;
          this.total = res.data.data.total;
        } else {
          this.$message({
            type: "error",
            message: res.data.msg,
            duration: 1000
          })
        }
      }).catch(error => {
        this.$message({
          type: "error",
          message: "系统错误，请联系管理员！",
          duration: 1000
        })
      })
    },
    cancelAddLeaveDialog(){
      this.addLeaveFormVisible = false;
      this.addLeaveForm = {}
    },
    showEditDialog(row){
    this.modifyLeaveForm = row;
    this.modifyLeaveFormVisible = true;
    },
    removeById(id){
      let url = this.$HRMSUrl+"/leave/del/"+id;
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$axios.delete(url)
            .then(res=>{
              if (res.data.code == 200){
                this.initLeave();
              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addLeave(){
      let url = this.$HRMSUrl + "/leave/add";
      console.log(this.addLeaveForm)
      this.$axios.post(url,this.addLeaveForm).then(
          res=>{
            if (res.data.code == 200){
              this.addLeaveFormVisible = false;
              this.addLeaveForm = {};
              this.initLeave();
            }else {
              this.$message({
                type:"error",
                message:res.data.msg,
                duration:1000
              })
            }
          }).catch(error=>{
        this.$message({
          type:"error",
          message:"系统错误，请联系管理员!",
          duration:1000
        })
      })
    },
    cancelModifyLeaveDialog(){
      this.modifyLeaveFormVisible = false;
    },
    modifyLeave(){
      let url = this.$HRMSUrl+"/leave/modify";
      this.$axios.put(url,this.modifyLeaveForm)
          .then(res=>{
            if (res.data.code == 200){
              this.modifyLeaveFormVisible = false ;
              this.initLeave();
            }else {
              this.$message({
                type:"error",
                message:res.data.msg,
                duration:1000
              })
            }
          }).catch(error=>{
        this.$message({
          type:"error",
          message:"系统错误，请联系管理员!",
          duration:1000
        })
      })
    },

  },
  created() {
    this.initLeave();
  }
}
</script>

<style scoped>

</style>